package com.madixin.madichat.ui.navigation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.madixin.madichat.ui.feature.chat.ChatScreen
import com.madixin.madichat.ui.feature.chat.ChatViewModel
import com.madixin.madichat.ui.feature.login.LoginScreen
import com.madixin.madichat.ui.feature.login.LoginViewModel
import com.madixin.madichat.util.PreferencesManager
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch

@Composable
fun NavGraph(
    navController: NavHostController = rememberNavController(),
    preferencesManager: PreferencesManager
) {
    // 使用协程作用域来检查token
    val coroutineScope = rememberCoroutineScope()
    // 记录初始目标路由
    var startDestination by remember { mutableStateOf<String?>(null) }
    
    // 在组合时检查token并设置初始目标
    LaunchedEffect(key1 = true) {
        coroutineScope.launch {
            val token = preferencesManager.tokenFlow.first()
            startDestination = if (!token.isNullOrEmpty()) "chat" else "login"
        }
    }
    
    // 等待初始目标确定后再显示NavHost
    startDestination?.let { destination ->
        NavHost(
            navController = navController,
            startDestination = destination
        ) {
            composable("login") {
                val loginViewModel: LoginViewModel = viewModel(factory = remember {
                    object : androidx.lifecycle.ViewModelProvider.Factory {
                        override fun <T : androidx.lifecycle.ViewModel> create(modelClass: Class<T>): T {
                            return LoginViewModel(preferencesManager) as T
                        }
                    }
                })
                
                LoginScreen(
                    viewModel = loginViewModel,
                    onNavigateToChat = { navController.navigate("chat") { popUpTo("login") { inclusive = true } } }
                )
            }
            
            composable("chat") {
                val chatViewModel: ChatViewModel = viewModel(factory = remember {
                    object : androidx.lifecycle.ViewModelProvider.Factory {
                        override fun <T : androidx.lifecycle.ViewModel> create(modelClass: Class<T>): T {
                            return ChatViewModel(preferencesManager) as T
                        }
                    }
                })
                
                ChatScreen(
                    viewModel = chatViewModel,
                    onNavigateToLogin = { navController.navigate("login") { popUpTo("chat") { inclusive = true } } }
                )
            }
        }
    }
}